import 'package:flutter/material.dart';

import 'package:flutter_widgets_pc/widgets/button/kq_ink_well.dart';

import '../../config/kq_global.dart';
import '../../resources/kq_images.dart';
import '../../resources/kq_theme_colors.dart';
import '../../resources/l10n/kq_intl.dart';
import '../../theme/kq_theme_manager.dart';
import '../../utils/text_filed_utils.dart';
import '../../utils/text_input_format_utils.dart';
import '../divider/kq_divider.dart';
import '../textFiled/kq_textfiled.dart';
import 'entity/kq_form_entity.dart';

/// 通用表单组件 - 原因选择+数量
///
/// 左边删除图标 + 标题 + 选择原因 + 下拉图标 + 数量减按钮  + 数量输入框 + 数量加按钮
///
/// 推荐直接使用[KqAddNumberItemView]
///
/// 如果单独使用，需和[KqFormQtyEntity]使用
///
/// 标题：title
/// 选择提示：hint
/// 选择的显示值使用：valueForShow
/// 选择的提交值使用:valueForSubmit
/// 数字值使用：qty
///
/// @author 周卓
///
class KqNumberItemView extends StatefulWidget {
  /// 子控件数据
  final KqFormQtyEntity entity;

  /// 选择控件点击回调
  final Function(KqFormQtyEntity entity)? onTap;

  /// 数量组件删除按钮点击回调
  final Function(KqFormQtyEntity entity)? onRemove;

  const KqNumberItemView(
      {Key? key, required this.entity, this.onTap, this.onRemove})
      : super(key: key);

  @override
  KqNumberItemViewState createState() => KqNumberItemViewState();
}

class KqNumberItemViewState extends State<KqNumberItemView> {
  @override
  void initState() {
    widget.entity.widgetState = this;
    super.initState();
  }

  /// 主动刷新方法
  update() {
    if (mounted) {
      setState(() {});
    }
  }

  /// 点击减号
  void _onDec() {
    var qty = widget.entity.qty;
    if (qty > widget.entity.minQty) {
      _onValueChanged((widget.entity.qty - 1).toString());
    }
  }

  /// 点击加号
  void _onPlus() {
    _onValueChanged((widget.entity.qty + 1).toString());
  }

  /// 输入框数值变化
  void _onValueChanged(String text) {
    setState(() {
      widget.entity.isEmpty = text.isEmpty;
      widget.entity.qty = text.isEmpty ? widget.entity.minQty : int.parse(text);
      if (widget.entity.qty < widget.entity.minQty) {
        widget.entity.qty = widget.entity.minQty;
      }
    });
  }

  // 减号能不能点
  bool _decAble() {
    return widget.entity.qty > widget.entity.minQty;
  }

  @override
  Widget build(BuildContext context) {
    // 背景颜色
    var bgColor = widget.entity.backgroundColor ?? KqThemeColors.bgF4;
    return Container(
      color: bgColor,
      child: Column(
        children: [
          Container(
            padding: EdgeInsets.symmetric(horizontal: 16),
            child: Row(
              children: [
                /// 左边减号
                InkWell(
                  onTap: () {
                    if (widget.entity.removable) {
                      if (widget.entity.onRemove != null) {
                        widget.entity.onRemove!(widget.entity);
                      }
                      if (widget.onRemove != null) {
                        widget.onRemove!(widget.entity);
                      }
                    }
                  },
                  child: Container(
                      alignment: Alignment.centerRight,
                      height: 44,
                      child: widget.entity.removable
                          ? Image.asset(
                              KqImages.commonIcDelete16Red,
                              width: 15,
                              height: 15,
                              fit: BoxFit.contain,
                              package: KqGlobal.packageName,
                            )
                          : SizedBox(
                              width: 15,
                              height: 15,
                            )),
                ),
                SizedBox(
                  width: 12,
                  height: 1,
                ),

                /// 左边标题
                Expanded(
                    child: Row(
                  children: [
                    /// 标题
                    Visibility(
                      visible: widget.entity.title != null &&
                          widget.entity.title != '',
                      child: Container(
                        padding: EdgeInsets.only(right: 12),
                        margin: EdgeInsets.symmetric(vertical: widget.entity.paddingVertical ??
                            KqThemeManager.instance
                                .getConfig()
                                .formConfig
                                .paddingVertical ??
                            15),
                        child: Text(
                          widget.entity.title ?? '',
                          style: TextStyle(
                            fontSize: KqThemeManager.instance
                                    .getConfig()
                                    .formConfig
                                    .titleFontSize ??
                                16,
                            color: KqThemeColors.text26,
                          ),
                        ),
                      ),
                    ),
                    Expanded(
                        child: KqInkWell(
                      verticalPadding: widget.entity.paddingVertical ??
                          KqThemeManager.instance
                              .getConfig()
                              .formConfig
                              .paddingVertical ??
                          15,
                      onTap: () {
                        TextFiledUtil.clearFocus(context);
                        if (widget.entity.onTap != null) {
                          widget.entity.onTap!(widget.entity);
                        }
                        if (widget.onTap != null) {
                          widget.onTap!(widget.entity);
                        }
                      },
                      child: Row(
                        children: [
                          /// 选择的文字
                          Expanded(
                              child: Text(
                            widget.entity.valueForShow ??
                                (widget.entity.hint ??
                                    KqIntl.currentResource.pleaseChoose),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                              fontSize: KqThemeManager.instance
                                      .getConfig()
                                      .formConfig
                                      .contentFontSize ??
                                  16,
                              color: widget.entity.valueForShow != null
                                  ? KqThemeColors.text59
                                  : KqThemeColors.textBF,
                            ),
                          )),

                          /// 下拉箭头
                          Image.asset(
                            KqImages.commonIcZhankai16Grey,
                            width: 16,
                            height: 16,
                            package: KqGlobal.packageName,
                          )
                        ],
                      ),
                    )),
                    SizedBox(
                      width: 24,
                      height: 1,
                    ),

                    /// 数量减
                    InkWell(
                      onTap: () => _onDec(),
                      child: Image.asset(
                        _decAble()
                            ? KqImages.commonIcReduceNormal
                            : KqImages.commonIcReduceDisabled,
                        width: 32,
                        height: 26,
                        package: KqGlobal.packageName,
                      ),
                    ),
                    SizedBox(
                      width: 4,
                      height: 1,
                    ),

                    /// 数量输入框
                    SizedBox(
                      width: 46,
                      child: KqTextFiled(
                        inputType: TextInputType.number,
                        multiline: false,
                        maxLines: 1,
                        enableClearButton: false,
                        textAlign: TextAlign.center,
                        newValue: widget.entity.isEmpty
                            ? null
                            : widget.entity.qty.toString(),
                        fontSize: KqThemeManager.instance
                                .getConfig()
                                .formConfig
                                .contentFontSize ??
                            16,
                        defValue: widget.entity.qty.toString(),
                        placeHolder: widget.entity.minQty.toString(),
                        inputFormatters: TextInputFormatUtils.integer(),
                        onChanged: (value) => _onValueChanged(value),
                      ),
                    ),
                    SizedBox(
                      width: 4,
                      height: 1,
                    ),

                    /// 数量加
                    InkWell(
                      onTap: () => _onPlus(),
                      child: Image.asset(
                        KqImages.commonIcIncreaseNormal,
                        width: 32,
                        height: 26,
                        package: KqGlobal.packageName,
                      ),
                    ),

                    /// 单位
                    Visibility(
                        visible: widget.entity.unit != null,
                        child: Container(
                          alignment: Alignment.center,
                          padding: EdgeInsets.only(left: 10),
                          child: Text(
                            widget.entity.unit ?? '',
                            maxLines: 1,
                            style: TextStyle(
                                fontSize: KqThemeManager.instance
                                        .getConfig()
                                        .formConfig
                                        .unitFontSize ??
                                    14,
                                color: widget.entity.unitColor ?? KqThemeColors.text59),
                          ),
                        ))
                  ],
                ))
              ],
            ),
          ),
          const KqDivider(),
        ],
      ),
    );
  }
}
